<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * @author Justina Lee
 */
class eresources extends CI_Controller {
    
    function __construct() {
        parent::__construct();
        $this->load->model('eresources_model');
    }
/*
|--------------------------------------------------------------------------
| eResourcesList (@author : Justina Lee)
|--------------------------------------------------------------------------
|
| This function returns the parsed eResources results as JSON and call the 
| function to wrap the response in the requested callback. 
|
*/      
    public function eResourcesList() {     
        $xmlDatabaseUrl = $this->config->item('database_xml');
        $xmlCategoryUrl = $this->config->item('databaseCategory_xml');
        
        $output = $this->eresources_model->parseXml($xmlDatabaseUrl, $xmlCategoryUrl);
        $this->retJsonP($output);
    }
	
/*
|--------------------------------------------------------------------------
| eResourcesMusicList (@author : Sean Yow)
|--------------------------------------------------------------------------
|
| This function returns the parsed eResources Music results as JSON and call the 
| function to wrap the response in the requested callback. 
|
*/      
    public function eResourcesMusicList() {     
        $xmlDatabaseUrl = $this->config->item('music_database_xml');
        $xmlCategoryUrl = $this->config->item('music_databaseCategory_xml');
        
        $output = $this->eresources_model->parseXml($xmlDatabaseUrl, $xmlCategoryUrl);
        $this->retJsonP($output);
    }
/*
|--------------------------------------------------------------------------
| eResourcesCategories (@author : Justina Lee)
|--------------------------------------------------------------------------
|
| This function returns the parsed eResources categories results as JSON and 
| call the function to wrap the response in the requested callback. 
|
*/  
    public function eResourcesCategories(){
        $xmlCategoryUrl = $this->config->item('databaseCategory_xml');
        
        $output = $this->eresources_model->parseCategoryXml($xmlCategoryUrl);
        $this->retJsonP($output);
    }

/*
|--------------------------------------------------------------------------
| eResourcesMusicCategory (@author : Justina Lee)
|--------------------------------------------------------------------------
|
| This function returns the parsed eResources Music categories results as JSON and 
| call the function to wrap the response in the requested callback. 
|
*/  
    public function eResourcesMusicCategory(){
        $xmlCategoryUrl = $this->config->item('music_databaseCategory_xml');
        
        $output = $this->eresources_model->parseCategoryXml($xmlCategoryUrl);
        $this->retJsonP($output);
    }    
/*
|--------------------------------------------------------------------------
| retJsonP (@author : Justina Lee)
|--------------------------------------------------------------------------
|
| This function wraps the response in the requested callback. 
|
*/
    function retJsonP($output) {
        $callback = $_REQUEST['callback'];

        if ($callback) {
            header('Content-Type: text/javascript');
            echo $callback . '(' . json_encode($output) . ');';
        } else {
            header('Content-Type: application/x-json');
            echo json_encode($output);
        }
    }
}

?>
